package user

import (
	"go-web-scaffold/api"
	"go-web-scaffold/dao"
	"go-web-scaffold/service"
	"go-web-scaffold/tws"

	"github.com/gin-gonic/gin"
)

func RegisterController(engine *gin.Engine, routerManager *tws.RouterManager, instance *api.SqliteDB) {
	routerManager.Register("user:register:post", func(ctx *tws.Context) {
		registerUser(ctx, instance)
	})
}

func registerUser(ctx *tws.Context, instance *api.SqliteDB) {
	userName := ctx.Context.PostForm("userName")
	password := ctx.Context.PostForm("password")
	userDao := dao.NewUserDao(instance)
	service := service.NewUserService(userDao)
	if userName == "" || password == "" {
		ctx.Context.JSON(200, gin.H{
			"status": "error",
			"result": "注册信息不完整",
		})
	} else {
		registerInfo := service.RegisterUser(userName, password)
		ctx.Context.JSON(200, gin.H{
			"status": registerInfo.Status,
			"result": registerInfo.Msg,
		})
	}
}
